<?php 
session_start();
require_once ("class/m_usergroup.php");

	$m = new M_usergroup();
	$user=$_SESSION['username'];
	
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				
				$type=$_POST['type'];
				//get header
				$header=$m->getDataDetail($id);
				//get detail
				$result=$m->UserPermission($id);
				
				$rows=$m->getRowCount();
				$irow=1;
				$html="";
				
				while($row = $result->FetchRow()){
					$html .= genUserPemission($row,$irow);
					$irow++;
				}
			
				$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'header'=> json_encode( $header )
							);			
				echo json_encode($ar_result);
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']== 'add'){
			$col= $_POST['var'];
			
			//insert to tb_usergroup
			$ar["GroupName"]=$col["groupname"];
			$ar["Type"]=$col["type"];
			$ar["Discount"]=$col["discount"];
			$ar["CreatedBy"]=$user;
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			
			$list=$col['list'];
			$id=$m->InsertData($ar);
			
			$sql=" Insert into tb_group_permission (`GroupID` ,`ScreenID` ,`View` ,`Add` ,`Edit` ,`Deleted`  ) values ";
			$sqlDetail="";
			//insert to tb_group_permission
			foreach($list as $row){
					$sqlDetail .=",(".$id.",".$row["id"].",'".$row["view"]."','".$row["add"]."','".$row["edit"]."','".$row["del"]."')";
			}
			
			 //remove first comma
			$sqlDetail = substr($sqlDetail,1);
			//Insert data
			//echo $sql.$sqlDetail; 
			$m->Excute($sql.$sqlDetail);	
			
			echo "<tr>".genTable($m->getDataDetail($id),$_POST['rows']+1)."</tr>";
			
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$col= $_POST['var'];
				$list=$col['list'];
				
				$id=$_POST['id'];
				//insert to tb_usergroup
				$ar["GroupName"]=$col["groupname"];
				$ar["Type"]=$col["type"];
				$ar["Discount"]=$col["discount"];
				$ar["UpdatedBy"]=$user;
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				//update user group
				$sqlScript="select * from tb_usergroup where 1=1 and GroupID='".$id."'";
				$m->updateData($sqlScript, $ar);
				
				//clear group permission
				$m->delePermission($id);
				
				//Insert new group
				$sql=" Insert into tb_group_permission (`GroupID` ,`ScreenID` ,`View` ,`Add` ,`Edit` ,`Deleted`  ) values ";
				$sqlDetail="";
				//insert to tb_group_permission
				foreach($list as $row){
						$sqlDetail .=",(".$id.",".$row["id"].",'".$row["view"]."','".$row["add"]."','".$row["edit"]."','".$row["del"]."')";
				}
				
				 //remove first comma
				$sqlDetail = substr($sqlDetail,1);
				//Insert data
				//echo $sql.$sqlDetail; 
				$m->Excute($sql.$sqlDetail);	
				
				echo genTable($m->getDataDetail($id),$_POST['rows']);
			}
					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$r=$m->delData($o["id"]);

			}
		}else if ($_POST['m']== 'authen'){
			$type=$_POST['type'];
			
			$result=$m->getPermission($type);
			$rows=$m->getRowCount();
			$irow=1;
			$html="";
			while($row = $result->FetchRow()){
				$html .= genPemissionTable($row,$irow , true);
				$irow++;
			}
			
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							);
			echo json_encode( $ar_result );

		}else if ($_POST['m']== 'search'){
			$name=$_POST['name'];
			$type=$_POST['type'];
			$page=$_POST['page'];
			
			$page=isset($page)?$page:1;	
			$result=$m->getData($name,$type,$page);
			
			$rows=$m->getRowCount();
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			if ($rows > 0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";    
					}
					$html .= genTable($row,$i)."</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan=8 align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
		}
	}else{ 
		if (isset($_GET['c']) && $_GET['c'] !="" ){
			echo json_encode( $m->getDataCBO($_GET['c']));
		}
	}
	function genUserPemission($row,$i){
		$View = "&nbsp;";
		$Add="&nbsp;";
		$Edit="&nbsp;";
		$Deleted="&nbsp;";
		
		if ($row["h_view"]=="1"){
			$check=$row["View"]=="1"? "checked" : "";
			$View= "<input type='checkbox' id='v_".$row['ID']."' name='view' value='1'  ". $check."/>";
		}
		if ($row["h_add"]=="1"){
			$check=$row["Add"]=="1"? "checked" : "";
			$Add= "<input type='checkbox' id='a_".$row['ID']."' name='add' value='1'  ". $check."/>";		
		}
		if ($row["h_edit"]=="1"){
			$check=$row["Edit"]=="1"? "checked" : "";
			$Edit= "<input type='checkbox' id='e_".$row['ID']."' name='edit' value='1'  ". $check."/>";	
		}
		if ($row["h_deleted"]=="1"){
			$check=$row["Deleted"]=="1"? "checked" : "";
			$Deleted= "<input type='checkbox' id='d_".$row['ID']."' name='delete' value='1'  ". $check."/>";	
		}
		$table="<tr><td align='center'>".$i."</td>" .
			"<td style='display:none'>".$row["ID"]."</td>" .
			"<td  align='left'>".$row["ScreenName"]."</td>" .
			"<td align='center'>".$View."</td>" .
			"<td align='center'>".$Add."</td>" .
			"<td align='center'>".$Edit."</td>" .
			"<td align='center'>".$Deleted."</td></tr>" ;
		return $table;		
	}
	
	function genPemissionTable($row,$i ,$Ischeck){
		$check= $Ischeck==true? "checked" : "";
		$checkView=$row["View"]=="1"? "<input type='checkbox' id='v_".$row['id']."' name='view' value='1'  ". $check."/>":"&nbsp;" ;
		$checkEdit=$row["Edit"]=="1"? "<input type='checkbox' id='e_".$row['id']."'  name='edit' value='1' ". $check."/>":"&nbsp;" ;
		$checkAdd=$row["Add"]=="1"? "<input type='checkbox' id='a_".$row['id']."'  name='add' value='1' ". $check." />":"&nbsp;" ;
		$checkDel=$row["Deleted"]=="1"? "<input type='checkbox' id='d_".$row['id']."'  name='delete' value='1' ". $check." />":"&nbsp;" ;
		
		$table="<tr><td align='center'>".$i."</td>" .
			"<td style='display:none'>".$row["id"]."</td>" .
			"<td  align='left'>".$row["ScreenName"]."</td>" .
			"<td align='center'>".$checkView."</td>" .
			"<td align='center'>".$checkAdd."</td>" .
			"<td align='center'>".$checkEdit."</td>" .
			"<td align='center'>".$checkDel."</td></tr>" ;
		return $table;
	}
	function genTable($row,$i){
		global $userGroupScreen;
		
		$s.="<td align='center'>".$i."</td>" .
			"<td style='display:none'>".$row["GroupID"]."</td>" .
			"<td align='center'>".$row["GroupName"]."</td>" .
			"<td align='center'>".$row["GroupType"]."</td>" .
			"<td align='center'>".$row["Discount"]."</td>" ;
		
		$s.="<td align='center' style='display:none'><img src='./images/view.png' class='btnView' style='cursor:pointer' title='View' border='0'/></td>";
		
		if ($_SESSION["permission"][$userGroupScreen]["Edit"]=="1")
			$s.="<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>";
		else 
			$s.="<td>&nbsp;</td>";
			
		if ($_SESSION["permission"][$userGroupScreen]["Deleted"]=="1")			
			$s.="<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" ;
		else 
			$s.="<td>&nbsp;</td>";
			
		return $s;
	}

?>